
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#pragma once

#include <list>
using std::list;

#include "Base.h"
#include "Source.h"

//include parameter here to avoid to have to put it in every specific controller
#include "Parameter.h"

/*Base class controller.												*/
/*Inherite from this class to create a specific controller.				*/
template <typename T> class Controller : public Source<T>
{
private:
	
	/* the output parameter of the controller */
	Parameter<T>		_output;	

	/* a liste of parameter used to compute the value of the controller */
	list<Base*>			_dependent; 

	/* indicate if the value of the output parameter has to be calculated*/
	bool				_dirty;		

public:
	/*Constructor*/
	Controller();

	/*Destructor*/
	virtual ~Controller();

	/*Register a parameter to the controller*/
	void registerDependent(Base*);

	/*calculate and return the value of the output parameter*/
	const T& getValue();

	/*set the value of the output parameter*/
	void setValue(T);

	/*return a pointer to the output parameter of the controller*/
	Parameter<T>* getOutputParameter();

	/*calculate the value of the output parameter. This function should never be called outside of this class.*/
	void onDemand();

	/*dirty the controller. should never be used outside of this class.*/
	void onChange();

private:
	/*update (calculate) the controller. This function should be overwritten to create a specific controller.*/ 
	virtual void doUpdate() = 0;
};


//constructor
template <typename T> Controller<T>::Controller()
:_dirty(true)//, _output()
{
	_output.init(this, 0, true);
	registerDependent(&_output);
}

//destructor
template <typename T> Controller<T>::~Controller()
{
	_dependent.clear();
}

//register a parameter
template <typename T> void Controller<T>::registerDependent(Base* p)
{
	_dependent.push_front(p);
}

//calculate and return the value of the output parameter
template <typename T> const T& Controller<T>::getValue()
{
	return _output.getValue();
}

//set the value of the output parameter
template <typename T> void Controller<T>::setValue(T value)
{
	_output.setValue(value);
}

//return a pointer to the output parameter
template <typename T> Parameter<T>* Controller<T>::getOutputParameter()
{
	return &_output;
}

//update the controller
template <typename T> void Controller<T>::onDemand()
{
	if (_dirty)
    {
        doUpdate();
        _dirty = false;
    }
}

//dirty the controller
template <typename T> void Controller<T>::onChange()
{
	if (_dirty)
        return;		

    _dirty = true;
    for ( list<Base*>::iterator it = _dependent.begin(); it != _dependent.end(); it++ )
        (*it)->onChange();
}